package com.zz.load.http;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MyRequestServlet {
    private String method;
    private String path;
    private Map<String, String> kvMap = new HashMap<>();
    public MyRequestServlet(String resource){
        //GET /login?username=zhangzhe&password=123456 HTTP/1.1
        String[] arr = resource.split(" ");
        //解析请求方式
        this.method = arr[0];
        //解析请求携带的参数
        if(arr[1].indexOf("?") != -1){
            path = arr[1].substring(0, arr[1].indexOf("?"));
            String[] resourceArr = arr[1].substring(arr[1].indexOf("?")+1).split("&");
            for (String kvStr : resourceArr) {
                String[] kv = kvStr.split("=");
                kvMap.put(kv[0], kv[1]);
            }

        }else{
            path = arr[1].substring(0);
        }
    }

    public String getMethod() {
        return method;
    }

    public String getPath() {
        return path;
    }

    public String getParameter(String k){
        return kvMap.get(k);
    }
}
